import InnovationStudioController from '@/services/InnovationStudio/InnovationStudioController';
import { message, Select, Transfer } from 'antd';
import React from 'react';
const {Option}=Select
class NewProjectTow extends React.Component {
    constructor(props) {
        super(props);
        let storageData = JSON.parse(sessionStorage.getItem('newLeanProject_two'))
        let userInfo = JSON.parse(sessionStorage.getItem("userInfo"))
        let level = '';
        if(userInfo.roleName.includes("市级")){
            level="市级"
        }
        if(userInfo.roleName.includes("省级")){
            level="省级"
        }
        this.state = { 
            targetKeys:[],
            selectedKeys:[],
            studioList:[],
            level:level,
            membersList:[],
            projectLeaderName: undefined,
            projectLeaderUserName:undefined,
            selectedStudio:undefined
         };
    }
    componentDidMount(){
        this.props.onRef(this)
        this.getStudioList()

    }
    getStudioList=()=>{
        let {level} = this.state
        InnovationStudioController.getList({level,studioStatus:'正常'}).then(r=>{
            if(r&&r.data&&r.data.rows&&r.data.rows.length>0){
                this.setState({
                    studioList:r.data.rows
                })
            }
            else{
                message.error("没有可选择的工作室")
            }
        })
    }
    handleChange=(nextTargetKeys, direction, moveKeys)=>{
        this.setState({ targetKeys: nextTargetKeys });
    }
    handleSelectChange=(sourceSelectedKeys, targetSelectedKeys)=>{
        this.setState({ selectedKeys: [...sourceSelectedKeys, ...targetSelectedKeys] });
    }
    handleScroll=()=>{}
    selectChange=(e)=>{
      this.getMembersByList(e)
        this.setState({
            projectStudio:e
        })
    }
    getMembersByList=(studioList)=>{
        InnovationStudioController.getMembersByList({studioList:studioList}).then(r=>{
            if(r&&r.data&&r.data.length>0){
                let targetKeys = []
                let data = r.data.map(item=>{
                    item.key = item.userName
                    targetKeys.push(item.userName)
                    return item
                })
                
                this.setState({
              
                    membersList:data,
                    targetKeys:targetKeys
                })
            }
            else{
                message.error('没有查询到工作室成员')
            }
        })
    }
    dealerChange=(e)=>{
        let datas = e.split("/")
        this.setState({projectLeaderUserName:datas[0],projectLeaderName:datas[1]})
    }
    saveDatas=()=>{
        let {projectLeaderName,projectLeaderUserName,targetKeys,projectStudio} = this.state
        if(!projectLeaderUserName||!projectLeaderName){
            message.error('请选择负责人')
            return;
        }
        if(targetKeys.length===0){
            message.error('至少选择一个项目成员')
            return;
        }
        let datas = {projectLeaderName,projectLeaderUserName,projectMembers:targetKeys,projectStudio}
        sessionStorage.setItem('newLeanProject_two',JSON.stringify(datas))
        return true

    }
    render() {
        const {targetKeys,selectedKeys,studioList,membersList,projectLeaderUserName,projectLeaderName,selectedStudio}=this.state
        return (
            <div><div style={{margin:5}}>
                <strong>选择参与部门：</strong>
                <Select onChange={this.selectChange} mode="multiple" style={{width:400}} placeholder={'请选择'} value={selectedStudio}>
                    {studioList&&studioList.map(item=><Option value={item.studioCode}>{item.studioName}</Option>)}
                </Select>
                </div>
                <div style={{margin:5}}>
                <strong>负责人的姓名：</strong>
                <Select onChange={this.dealerChange} style={{width:400}} placeholder={'请选择'} value={projectLeaderUserName?projectLeaderUserName+'/'+projectLeaderName:projectLeaderUserName}>
                    {membersList&&membersList.map((item,index)=><Option value={item.userName+'/'+item.name} key={index}>{item.name}</Option>)}
                </Select>
                </div>
                <strong>选择项目成员：</strong>
                <Transfer
                dataSource={membersList.filter(item=>item.key!==projectLeaderUserName)}
                titles={['未选择','已选择']}
                targetKeys={targetKeys}
                selectedKeys={selectedKeys}
                onChange={this.handleChange}
                onSelectChange={this.handleSelectChange}
                onScroll={this.handleScroll}
                render={item => item.name}
                style={{width:600}}
                ></Transfer>
            </div>
        );
    }
}

export default NewProjectTow;